using UnityEngine;
using QFramework;
using UnityEngine.UI;




namespace QFramework.Example
{
    /// <summary>
    /// 伤害字体飘出
    /// </summary>
	public partial class FloatingTextController : ViewController
	{
        static FloatingTextController instance;

        void Start()
		{
            FloatingText.Hide();
        }
        public static void Play(Vector2 pos, string text, bool critical = false)
        {
            ///生成一个
            instance.FloatingText.InstantiateWithParent(instance.transform).Position(pos.x, pos.y).Self(self =>
            {


                var positionY = pos.y;
                var textTrabs = self.transform.Find("Text");

                var textComp = textTrabs.GetComponent<Text>();

                textComp.text = text;


                if (critical)
                {
                    textComp.color = Color.red;

                }


                ActionKit.Sequence().Lerp(0, 0.5f, 0.5f, (p) =>
                {
                    self.PositionY(positionY + p * 0.35f);
                    textComp.LocalScaleX(Mathf.Clamp01(p * 8));
                    textComp.LocalScaleY(Mathf.Clamp01(p * 8));

                }).Delay(0.3f, () =>
                {

                }).Lerp(1.0f, 0, 0.3f, p =>
                {
                    textComp.ColorAlpha(p);

                }, () =>
                {
                    self.DestroyGameObjGracefully();

                }).Start(textComp);


            }).Show();

        }

        private void Awake()
        {
            instance = this;
        }

        private void OnDestroy()
        {
            instance = null;
        }
    }
}
